﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Security;
namespace AMS.Inventory.PCClient.InventoryHelpClass
{
    public class publ
    {
        private static SymmetricAlgorithm mCSP;


        public static byte[] DESKey = new byte[] { 0x82, 0xBC, 0xA1, 0x6A, 0xF5, 0x87, 0x3B, 0xE6, 0x59, 0x6A, 0x32, 0x64, 0x7F, 0x3A, 0x2A, 0xBB, 0x2B, 0x68, 0xE2, 0x5F, 0x06, 0xFB, 0xB8, 0x2D, 0x67, 0xB3, 0x55, 0x19, 0x4E, 0xB8, 0xBF, 0xDD };

        //对字符串进行MD5加密
        public static string MD5(string Str_Temp)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(Str_Temp, "MD5");
        }

        public static string VitEnctyptString(string sEnctString)
        {
            mCSP = SetEnc();
            byte[] byt1 = Convert.FromBase64String("tkGGRmBErvc="); //设置加密Key
            mCSP.Key = byt1;

            byte[] byt2 = Convert.FromBase64String("Kl7ZgtM1dvQ="); //设置加密IV
            mCSP.IV = byt2;

            //加密
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
            byt = Encoding.UTF8.GetBytes(sEnctString);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();
            return Convert.ToBase64String(ms.ToArray());

        }

        public static string VitDecryptString(string sDecryString)
        {
            mCSP = SetEnc();
            byte[] byt1 = Convert.FromBase64String("tkGGRmBErvc="); //设置加密Key
            mCSP.Key = byt1;

            byte[] byt2 = Convert.FromBase64String("Kl7ZgtM1dvQ="); //设置加密IV
            mCSP.IV = byt2;

            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            byt = Convert.FromBase64String(sDecryString);
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Encoding.UTF8.GetString(ms.ToArray());

        }





        private static SymmetricAlgorithm SetEnc()
        {
            return new DESCryptoServiceProvider();
        }

        //去除不规范字符
        public static string GetCatchMsg(string Str_Temp)
        {
            return Str_Temp.Replace("\'", "").Replace("\n", "").Replace("\r", "");
        }


        //地址栏参数发送转换
        public static string GetUrlToSend(string Str_Temp)
        {
            //无加密仅做转换
            //return System.Web.HttpUtility.UrlEncode(Str_Temp);


            //加密发送参数
            return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(Str_Temp));
        }

        //地址栏参数接收转换
        public static string GetUrlToReceive(string Str_Temp)
        {
            //无加密仅做转换
            //return System.Web.HttpUtility.UrlDecode(Str_Temp);


            //加密接受参数
            return System.Text.Encoding.Default.GetString(Convert.FromBase64String(Str_Temp));
        }
    }
}